package com.cong.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cong.project.common.BaseResponse;
import com.cong.project.common.ErrorCode;
import com.cong.project.common.ResultUtils;
import com.cong.project.exception.BusinessException;
import com.cong.project.model.entity.Content;
import com.cong.project.model.entity.PiazzaType;
import com.cong.project.model.enums.ContentTypeEnum;
import com.cong.project.model.request.*;
import com.cong.project.model.vo.PostDetailVo;
import com.cong.project.model.vo.PostVo;
import com.cong.project.service.CollectService;
import com.cong.project.service.ContentService;
import com.cong.project.service.LikeService;
import com.cong.project.service.PiazzaTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 帖子
 *
 * @author lhc
 * @date 2022-10-11 15:23
 */
@RestController
@RequestMapping("post")
public class PostController {

    @Resource
    private ContentService contentService;
    @Resource
    private LikeService likeService;
    @Resource
    private CollectService collectService;

    @Resource
    private PiazzaTypeService piazzaTypeService;

    /**
     * 用户帖子列表
     */
    @GetMapping("/list")
    public BaseResponse getPostList(TokenUserRequest tokenUserRequest, PostQueryRequest postQueryRequest) {
        IPage<PostVo> postVoList =  contentService.getPostList(tokenUserRequest,postQueryRequest);
        return ResultUtils.success(postVoList);
    }
    /**
     * 帖子点赞
     */
    @GetMapping("/postThumbUp")
    public BaseResponse postThumbUp(TokenUserRequest tokenUserRequest,Long postId) {
        if (postId<=0){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Content contentResult = contentService.getById(postId);
        if (contentResult==null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"无此帖子");
        }
        Integer thumbResult =  likeService.thumbUp(tokenUserRequest,postId, ContentTypeEnum.POST.getCode());
        return ResultUtils.success(thumbResult);
    }
    /**
     * 帖子收藏
     */
    @GetMapping("/postCollectUp")
    public BaseResponse postCollectUp(TokenUserRequest tokenUserRequest,Long postId) {
        if (postId<=0){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Content contentResult = contentService.getById(postId);
        if (contentResult==null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"无此帖子");
        }
        Integer collectResult =  collectService.collectUp(tokenUserRequest,postId, ContentTypeEnum.POST.getCode());
        return ResultUtils.success(collectResult);
    }

    /**
     * 添加文章
     *
     * @param tokenUserRequest 令牌用户请求
     * @param postAddRequest   post添加请求
     * @return {@link BaseResponse}
     */
    @PostMapping("/add")
    public BaseResponse addPost(TokenUserRequest tokenUserRequest, @RequestBody  PostAddRequest postAddRequest){
        if (tokenUserRequest.getId()==null||tokenUserRequest.getId()<=0){
            throw new BusinessException(ErrorCode.TOKEN_ERROR,"非法登录");
        }
        Boolean result = contentService.addPost(tokenUserRequest,postAddRequest);
        return ResultUtils.success(result);
    }
    @PostMapping("/delete")
    public BaseResponse deletePost(TokenUserRequest tokenUserRequest, @RequestBody PostDeleteRequest postDeleteRequest){
        if (tokenUserRequest.getId()==null||tokenUserRequest.getId()<=0){
            throw new BusinessException(ErrorCode.TOKEN_ERROR,"非法登录");
        }
        Boolean result = contentService.deletePost(tokenUserRequest,postDeleteRequest);
        return ResultUtils.success(result);
    }


    /**
     * 获取帖子类型
     */
    @GetMapping("/type")
    public BaseResponse getType() {

        List<PiazzaType> list = piazzaTypeService.list();
        return ResultUtils.success(list);
    }

    /**
     * 获取帖子详情
     */
    @GetMapping("/detail")
    public BaseResponse getDetail(Long postId,TokenUserRequest tokenUserRequest) {

       PostDetailVo postVo=  contentService.getPostDetail(tokenUserRequest,postId);
        return ResultUtils.success(postVo);
    }

}
